﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Collections.Generic;

namespace SP_XHWeb.WebParts.EditEmployee
{
    public partial class EditEmployeeUserControl : UserControl
    {
        public string AddEmployeeUrl = "/companyinfo/Lists/EmployeeInfo/NewForm.aspx";
        public string EditEmployeeUrl = "/companyinfo/Lists/EmployeeInfo/EditForm.aspx?ID=";
        public string pname = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            string absolPath = this.Page.Request.Url.AbsolutePath.ToLower();
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            string[] str = absolPath.Substring(1).Split('/');
            AddEmployeeUrl = hostUrl + AddEmployeeUrl;
            EditEmployeeUrl = hostUrl + EditEmployeeUrl;

            if (str != null && str.Length > 2 && str[0] == "projectinfo" && str[1] != "")
            {
                using (SPSite site = new SPSite(hostUrl + "/" + str[0] + "/" + str[1]))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        pname = web.Title.Trim().Replace("项目", "");
                        
                    }
                }
            }
            
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            tdEdit.Style.Add("display", "block");
            List<EmployeeItem> lists = new List<EmployeeItem>();
            string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
            using (SPSite site = new SPSite(hostUrl + "/companyinfo/default.aspx"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists.TryGetList("EmployeeInfo");
                    if (list != null && list.ItemCount != 0)
                    {
                        //公司不为空
                        if (!string.IsNullOrEmpty(pname))
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                if (item["公司"].ToString().Trim().StartsWith(pname))
                                {
                                    lists.Add(new EmployeeItem(item.ID, item["姓名"].ToString().Trim(), item["公司"].ToString().Trim()));
                                }
                            }
                        }
                    }
                }
            }
            if (lists != null && lists.Count > 0)
            {
                ddlName.DataSource = lists;
                ddlName.DataTextField = "Name";
                ddlName.DataValueField = "EID";
                ddlName.DataBind();

                EditEmployeeUrl += ddlName.Items[0].Value;
            }
            
        }

        protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ddlName.SelectedValue))
            {
                EditEmployeeUrl += ddlName.SelectedValue;
            }
            else
            {
                EditEmployeeUrl += ddlName.Items[0].Value;
            }
        }

        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string employeeID = "";
            if (!string.IsNullOrEmpty(ddlName.SelectedValue))
            {
                employeeID = ddlName.SelectedValue;
            }
            else
            {
                employeeID = ddlName.Items[0].Value;
            }
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                if (!string.IsNullOrEmpty(employeeID))
                {
                    string hostUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
                    using (SPSite site = new SPSite(hostUrl + "/companyinfo/default.aspx"))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;
                            SPList list = web.Lists.TryGetList("EmployeeInfo");
                            if (list != null && list.ItemCount != 0)
                            {
                                //公司不为空
                                if (!string.IsNullOrEmpty(pname))
                                {
                                    foreach (SPListItem item in list.Items)
                                    {
                                        if (item["公司"].ToString().Trim().StartsWith(pname) && item.ID == Int32.Parse(employeeID))
                                        {
                                            item.Delete();
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            });
            Response.Redirect(this.Page.Request.Url.ToString());
        }
        
    }

    public class EmployeeItem
    {
        public int EID { get; set; }

        public string Name { get; set; }

        public string Company { get; set; }

        public EmployeeItem(int eid, string name,string company)
        {
            this.EID = eid;
            this.Name = name;
            this.Company = company;
        }
    }
}
